#include "headfiles.h"

using namespace std;

bool removeNode(NODE *&head, NODE *n) {
	if (head == NULL) {
		cout << "Empty link list!" << endl;
		return false;
	}

	if (head == n) {
		head = head->next;
		free(n);
	}
	else {
		NODE *n_pre = head;
		while (n_pre->next) {
			if (n_pre->next == n) {
				break;
			}
			n_pre = n_pre->next;
		}

		if (n_pre->next == NULL) {
			cout << "Node is not in the link list!" << endl;
			return false;
		}

		n_pre->next = n->next;
		free(n);
	}

	return true;
}

bool removeDuplicate(NODE *head) {
	if (head == NULL) {
		cout << "Empty link list!" << endl;
		return false;
	}

	NODE *n1, *n2;
	n1 = head;
	while (n1) {
		n2 = n1->next;
		while (n2) {
			if (n1->val == n2->val) {
				NODE *tmp = n2->next;
				removeNode(head, n2);
				n2 = tmp;
			}
			else {
				n2 = n2->next;
			}
		}
		n1 = n1->next;
	}

	return true;
}

